package com.uwlaser.service.impl;

import com.uwlaser.base.PageInfo;
import com.uwlaser.domain.AntonStatisticsParam;
import com.uwlaser.service.AntonStatisticsService;
import com.uwlaser.service.dto.AntonStatisticsDto;
import com.uwlaser.service.mapper.AntonStatisticsMapper;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @author yangwei
 * @create 2022/12/30
 */
@Service
@AllArgsConstructor
public class AntonStatisticsServiceImpl implements AntonStatisticsService {
    @Autowired
    private AntonStatisticsMapper antonStatisticsMapper;

    @Override
    public PageInfo<AntonStatisticsDto> queryByParam(AntonStatisticsParam query, Pageable pageable) {
        //参数开始时间和结束时间不为空 赋值
        if(!CollectionUtils.isEmpty(query.getCreateTime())){
            String beginTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(query.getCreateTime().get(0));
            String endTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(query.getCreateTime().get(1));
            query.setBeginTime(beginTime);
            query.setEndTime(endTime);
        }
        query.setPageSize(pageable.getPageSize());
        query.setPageNumber(pageable.getPageNumber()*pageable.getPageSize());
        PageInfo<AntonStatisticsDto> res = new PageInfo<>();
        List<AntonStatisticsDto> antonStatisticsDtos = antonStatisticsMapper.queryByParam(query);
        Long count = antonStatisticsMapper.selectCountByParam(query);
        res.setContent(antonStatisticsDtos);
        res.setTotalElements(count);
        return res;
    }
}
